//
//  UITabBar+Static.m
//  DEMO3
//
//  Created by J on 16/5/7.
//  Copyright © 2016年 J. All rights reserved.
//

#import "UITabBar+Static.h"
#import <objc/runtime.h>
#import <objc/message.h>

@implementation UITabBar (Static)
+ (void)load{
    
    
    
    
    method_exchangeImplementations(class_getInstanceMethod(UITabBar.class, @selector(setDelegate:)), class_getInstanceMethod(self, @selector(setTrackingDelegate:)));
        
}

- (void)setTrackingDelegate:(id<UITabBarDelegate>)delegate{
 
    Class class = [delegate class];


    if (class_addMethod(class, NSSelectorFromString(@"setTrackingSelectedItem"), (IMP)setTrackingSelectedItem, "")) {
        
        
        Method dis_originalMethod = class_getInstanceMethod(class, NSSelectorFromString(@"setTrackingSelectedItem"));
        
        Method dis_swizzledMethod = class_getInstanceMethod(class, @selector(tabBar:didSelectItem:));
        
        //交换实现
        
        method_exchangeImplementations(dis_originalMethod, dis_swizzledMethod);
        
    }
    

}


void setTrackingSelectedItem(id self, SEL _cmd, id tabbar, id selectedItem)
{
    SEL selector = NSSelectorFromString(@"setTrackingSelectedItem");
    ((void(*)(id, SEL,id, id))objc_msgSend)(self, selector, tabbar, selectedItem);

    
    //此处添加你想统计的打点事件
    NSLog(@"你现在正在点击的是行%@ %@ %@",NSStringFromClass([self class]),((UITabBarItem *)selectedItem).title,(UITabBarItem*)selectedItem);
}

@end
